// Once the api loads call enable the search box.
function handleAPILoaded() {
  $('#search-button').attr('disabled', false);
}

function printApplicationDataFolderMetadata() {
	  var request = gapi.client.drive.files.get({
	    'fileId': 'appdata'
	  });
	  request.execute(function(resp) {
	    console.log('Id: ' + resp.id);
	    console.log('Title: ' + resp.title);
	  });
	}

function create() {
	console.log("create");
	  const boundary = '-------314159265358979323846';
	  const delimiter = "\r\n--" + boundary + "\r\n";
	  const close_delim = "\r\n--" + boundary + "--";

	  var reader = new FileReader();
	  reader.readAsBinaryString("test");
	  reader.onload = function(e) {
	    var contentType = fileData.type || 'application/octet-stream';
	    var metadata = {
	      'title': fileData.fileName,
	      'mimeType': contentType,
	      'parents': [{'id': 'appdata'}]
	    };

	    var base64Data = btoa(reader.result);
	    var multipartRequestBody =
	        delimiter +
	        'Content-Type: application/json\r\n\r\n' +
	        JSON.stringify(metadata) +
	        delimiter +
	        'Content-Type: ' + contentType + '\r\n' +
	        'Content-Transfer-Encoding: base64\r\n' +
	        '\r\n' +
	        base64Data +
	        close_delim;

	    var request = gapi.client.request({
	        'path': '/upload/drive/v2/files',
	        'method': 'POST',
	        'params': {'uploadType': 'multipart'},
	        'headers': {
	          'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
	        },
	        'body': multipartRequestBody});
	    if (!callback) {
	      callback = function(file) {
	        console.log(file);
	      };
	    }
	    request.execute();
	  };
}

// Search for a given string.
function search() {
  var q = $('#query').val();
  var request = gapi.client.youtube.search.list({
    q: q,
    part: 'snippet'
  });

  request.execute(function(response) {
    var str = JSON.stringify(response.result);
    console.log(str);
    $('#search-container').html('<pre>' + str + '</pre>');
  });
}